/*****************************************************************************/
/**
 *  @file   ResourceLabelWidget.cpp
 *  @author Naohisa Sakamoto
 */
/*----------------------------------------------------------------------------
 *
 *  Copyright (c) Visualization Laboratory, Kyoto University.
 *  All rights reserved.
 *  See http://www.viz.media.kyoto-u.ac.jp/kvs/copyright/ for details.
 *
 *  $Id$
 */
/*****************************************************************************/
#include "ResourceLabelWidget.h"
#include "DataSet.h"
#include "ResourceMonitor.h"
#include "Util.h"
#include <kvs/glut/Screen>
#include <string>


ResourceLabelWidget::ResourceLabelWidget( kvs::glut::Screen* screen ):
    kvs::glut::Label( screen )
{
    setMargin( 10 );
//    setSize( 280, 250 );
//    setTextColor( kvs::RGBColor( 128, 128, 128 ) );
//    setBackgroundColor( kvs::RGBAColor( 0, 0, 0, 0.5f ) );
}

void ResourceLabelWidget::attachResourceMonitor( ResourceMonitor* resource_monitor )
{
    m_resource_monitor = resource_monitor;
}

void ResourceLabelWidget::attachDataSet( DataSet* data_set )
{
    m_data_set = data_set;
}

void ResourceLabelWidget::update()
{
    std::string ncpus = Util::NumberToString( m_resource_monitor->numberOfCPUs() );
    std::string ngpus = Util::NumberToString( m_resource_monitor->numberOfGPUs() );
    std::string total_cpu_memory = Util::DataSizeWithUnit( m_resource_monitor->totalCPUMemory() );
    std::string free_cpu_memory = Util::DataSizeWithUnit( m_resource_monitor->freeCPUMemory() );
    std::string total_gpu_memory = Util::DataSizeWithUnit( m_resource_monitor->totalGPUMemory() );
    std::string free_gpu_memory = Util::DataSizeWithUnit( m_resource_monitor->freeGPUMemory() );

    std::string dimx = Util::NumberToString( m_data_set->volumeResolution().x() );
    std::string dimy = Util::NumberToString( m_data_set->volumeResolution().y() );
    std::string dimz = Util::NumberToString( m_data_set->volumeResolution().z() );
    std::string volume_resolution = dimx + " x " + dimy + " x " + dimz;
    std::string volume_data_type = m_data_set->volumeDataType();
    std::string volume_data_size = Util::DataSizeWithUnit( m_data_set->volumeDataSize() );
    std::string nparticles = Util::NumberWithCommas( m_data_set->numberOfParticles() );
    std::string particle_data_size = Util::DataSizeWithUnit( m_data_set->particleDataSize() );

    setText( "CPU RESOURCES" );
    addText( ( "  Cores  " + ncpus ).c_str() );
    addText( ( "  Total  " + total_cpu_memory ).c_str() );
    addText( ( "  Free   " + free_cpu_memory ).c_str() );
    addText( "" );
    addText( "GPU RESOURCES" );
    addText( ( "  Cores  " + ngpus ).c_str() );
    addText( ( "  Total  " + total_gpu_memory ).c_str() );
    addText( ( "  Free   " + free_gpu_memory ).c_str() );
    addText( "" );
    addText( "ORIGINAL VOLUME" );
    addText( ( "  Resolution  " + volume_resolution ).c_str() );
    addText( ( "  Data type   " + volume_data_type ).c_str() );
    addText( ( "  Data size   " + volume_data_size ).c_str() );
    addText( "" );
    addText( "GENERATED PARTICLES" );
    addText( ( "  Particles   " + nparticles ).c_str() );
    addText( ( "  Data size   " + particle_data_size ).c_str() );
}

void ResourceLabelWidget::screenUpdated()
{
    this->update();
}
